package com.wj.algorithm.lintcode.erfen;

public class P74 {

    public int findFirstBadVersion(int n) {
        if(n <= 0) {
            return 0;
        }
        
        int from = 1;
        int end = n;
        int mid = (from + end) / 2;
        boolean isFirstBad = SVNRepo.isBadVersion(1);
        if(isFirstBad) {
            return 1;
        }
        boolean isLastBad = SVNRepo.isBadVersion(n);
        if(!isLastBad) {
            return 0;
        }
        while(mid > from && mid < end) {
            boolean isMidBad = SVNRepo.isBadVersion(mid);
            if(isMidBad) {
                if(mid - from == 1) {
                    return mid;
                }
                end = mid;
            } else {
                if(end - mid == 1) {
                    return end;
                }
                from = mid;
            }
            mid = (from + end) / 2;
        }
        
        return 0;
    }
    
    public static void main(String[] args) {
        System.out.println(new P74().findFirstBadVersion(10));
    }
}
